﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace LigerUi.MVC.DataAccess.CommonTools
{

    public class EntityUtil
    {
        /// <summary>
        /// 从一个对象中拷贝数据到另一个对象（属性名一致的才会拷贝）
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        public static void CloneData<TSource, TDestination>(TSource source, TDestination destination)
        {
            if (source != null)
            {
                PropertyInfo[] destinationProperties = destination.GetType().GetProperties();
                PropertyInfo[] sourceProperties = source.GetType().GetProperties();
                foreach (PropertyInfo property in destinationProperties)
                {
                    PropertyInfo sourceProperty = source.GetType().GetProperty(property.Name);
                    if (sourceProperty != null)
                    {
                        object value = sourceProperty.GetValue(source, null);
                        try
                        {
                            property.SetValue(destination, value, null);
                        }
                        catch { }
                    }
                }
            }
        }

        /// <summary>
        /// 从一个对象中拷贝数据到另一个对象（属性名一致的才会拷贝,用于泛型实体）
        /// </summary>
        /// <param name="source"></param>
        /// <param name="destination"></param>
        public static void CloneData<TSource, TDestination>(List<TSource> sourcelist, List<TDestination> destinationlist)
        {
            if (sourcelist != null)
            {
                for (int i = 0; i < sourcelist.Count; i++)
                {
                    destinationlist.Add(Activator.CreateInstance<TDestination>());
                    for (int x = 0; x < destinationlist.Count; x++)
                    {
                        PropertyInfo[] destinationProperties = destinationlist[destinationlist.Count - 1].GetType().GetProperties();
                        PropertyInfo[] sourceProperties = sourcelist[i].GetType().GetProperties();

                        foreach (PropertyInfo property in destinationProperties)
                        {
                            PropertyInfo sourceProperty = sourcelist[i].GetType().GetProperty(property.Name);
                            if (sourceProperty != null)
                            {
                                object value = sourceProperty.GetValue(sourcelist[i], null);
                                try
                                {
                                    property.SetValue(destinationlist[destinationlist.Count - 1], value, null);
                                }
                                catch { }
                            }
                        }
                        break;
                    }
                }
            }
        }


        public static string ComputeMD5(string str)
        {
            string strResult = string.Empty;
            using (System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create())
            {
                byte[] bytes = Encoding.UTF8.GetBytes(str);
                byte[] results = md5.ComputeHash(bytes);
                md5.Clear();
                foreach (var item in results)
                {
                    strResult += item.ToString("X2");
                }
            }
            return strResult.ToLower();
        }

        /// <summary>
        /// 取随机数的ASCII码前2位+MD5值作为客户端保存密码
        /// </summary>
        /// <param name="encrypt"></param>
        /// <returns></returns>
        public static string Encrypt(string encrypt)
        {
            Random r = new Random(DateTime.Now.Millisecond);
            //取随机数的ASCII码前2位
            string key = ((char)r.Next(65, 91)).ToString() + ((char)r.Next(65, 91)).ToString();
            return Encrypt(encrypt, key);
        }

        public static string Encrypt(string encrypt, string key)
        {
            return key + ComputeMD5(key + ComputeMD5(encrypt));
        }
    }
}
